const toolService = require('@/services/tool.service')
const JsonResult = require('@/utils/httpResponse.util')

class ToolController {
  async createToolClassify (req, response) {
    try {
      await toolService.createToolClassify(req.body)
      JsonResult.success({
        req,
        response,
        message: '创建成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '创建失败' })
    }
  }

  async updateToolClassify (req, response) {
    try {
      const { id, ...attrs } = req.body
      const data = await toolService.updateToolClassify(id, attrs)
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      JsonResult.success({
        req,
        response,
        message: '更新成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '更新失败' })
    }
  }

  async updateTool (req, response) {
    try {
      const { id, ...attrs } = req.body
      const data = await toolService.updateTool(id, attrs)
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      JsonResult.success({
        req,
        response,
        message: '更新成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '更新失败' })
    }
  }

  async findToolClassify (req, response) {
    try {
      const data = await toolService.findToolClassify()
      JsonResult.success({
        req,
        response,
        data,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询失败' })
    }
  }

  async createTool (req, response) {
    try {
      const data = await toolService.createTool(req.body)
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      JsonResult.success({
        req,
        response,
        message: '创建成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '创建失败' })
    }
  }

  async batchImportTool (req, response) {
    try {
      const { classifyId, json } = req.body
      const data = await toolService.batchImportTools(classifyId, json)
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      JsonResult.success({
        req,
        response,
        data,
        message: '批量导入完成'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '批量导入异常' })
    }
  }

  async findTools (req, response) {
    const name = req.query.name
    try {
      const data = await toolService.findTools(name)
      JsonResult.success({
        req,
        response,
        data,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询失败' })
    }
  }

  async deleteToolClassifys (req, response) {
    try {
      const ids = req.body.ids
      const data = await toolService.deleteToolClassifys(ids)
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      JsonResult.success({
        req,
        response,
        message: '删除成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '删除失败' })
    }
  }

  async deleteTools (req, response) {
    try {
      const ids = req.body.ids
      const data = await toolService.deleteTools(ids)
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      JsonResult.success({
        req,
        response,
        message: '删除成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '删除失败' })
    }
  }

  async findToolTree (req, response) {
    try {
      const userId = req.query.userId
      const data = await toolService.findToolTree(userId)
      JsonResult.success({
        req,
        response,
        data,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询失败' })
    }
  }
}

module.exports = new ToolController()
